/**
 * 
 */
package com.pmsco.pms.core.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.pmsco.pms.core.dao.PmsUserDao;
import com.pmsco.pms.core.domain.PmsUser;
import com.pmsco.pms.util.JQGridBuildWhereClause;

/**
 * @author Long Tran
 *
 */
@Transactional(propagation = Propagation.REQUIRES_NEW)
public class PmsUserDaoImpl extends HibernateDaoSupport implements PmsUserDao {
	
	public void saveOrUpdatePmsUser(PmsUser pmsUser) {
		getHibernateTemplate().merge(pmsUser);
		getHibernateTemplate().flush();
	}

	public List<PmsUser> listPmsUser(PmsUser searchTemplate) {
		List<PmsUser> ret = new ArrayList<PmsUser>();
		ret = getHibernateTemplate().findByExample(searchTemplate);
		return ret;
	}
	
	public List<PmsUser> listPmsUser(String searchField, String searchString, String searchOper, String sidx, String sord, Integer page, Integer rows) {
		List<PmsUser> ret = new ArrayList<PmsUser>();
		Criteria crit = this.getSession().createCriteria(PmsUser.class);
		if(searchOper != null && searchOper.length() > 0)
			crit.add(JQGridBuildWhereClause.getWhereClase(searchField, searchString, searchOper));
		if("desc".equalsIgnoreCase(sord))
			crit.addOrder(Order.desc(sidx));
		else
			crit.addOrder(Order.asc(sidx));
		crit.setFirstResult((page - 1) * rows);
		crit.setMaxResults(page * rows);
		ret = crit.list();
		return ret;
	}
	
	public Integer countListPmsUser(String searchField, String searchString, String searchOper) {
		Integer ret = new Integer(0);
		Criteria crit = this.getSession().createCriteria(PmsUser.class);
		if(searchOper != null && searchOper.length() > 0)
			crit.add(JQGridBuildWhereClause.getWhereClase(searchField, searchString, searchOper));
		crit.setProjection(Projections.rowCount());
		ret = (Integer)crit.uniqueResult();
		return ret;
	}

	public PmsUser loadPmsUser(PmsUser searchTemplate) {
		PmsUser ret = null;
		List<PmsUser> temp = new ArrayList<PmsUser>();
		temp = getHibernateTemplate().findByExample(searchTemplate);
		if(temp != null && temp.size() == 1)
			ret = temp.get(0);
		return ret; 
	}
}